﻿#include "OiPCH.hpp"

namespace Oi { namespace PLScannerCore {

    class StatusReaderSystemImpl : public Impl<StatusReaderSystem>
    {
    public:
        QMap<QString, QSharedPointer<StatusReader>> _readers;
    };

    bool ProductStatus::loadFromSys(ProductStatus& status, const QString name)
    {
        auto reader = StatusReaderSystem::instance()->reader(name);

        if(reader)
        {
            return reader->status(status);
        }
        else
        {
            return false;
        }
    }

    QSharedPointer<StatusReader> StatusReader::create(QString name)
    {
        return QSharedPointer<StatusReader>();
    }

    StatusReaderSystem::StatusReaderSystem()
    {
        OI_I(StatusReaderSystem);
    }

    StatusReaderSystem::~StatusReaderSystem()
    {
        OI_E(StatusReaderSystem);
        d->_readers.clear();
    }

    QSharedPointer<StatusReader> StatusReaderSystem::reader(QString name)
    {
        OI_F(StatusReaderSystem);
        if(d->_readers.contains(name))
        {
            return d->_readers[name];
        }
        else
        {
            auto reader = StatusReader::create(name);
            if (reader)
            {
                d->_readers[name] = reader;
                return reader;
            }

            return QSharedPointer<StatusReader>();
        }
    }

    void StatusReaderSystem::clear()
    {
        OI_F(StatusReaderSystem);
        d->_readers.clear();
    }

    void StatusReaderSystem::addReader(QString name, QSharedPointer<StatusReader> reader)
    {
        OI_F(StatusReaderSystem);

        d->_readers[name] = reader;
    }

    StatusReaderSystem* StatusReaderSystem::instance()
    {
        static StatusReaderSystem system;
        return &system;
    }

} }
